﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RemoteObject;
using System.Threading;

namespace RemoteClient.UI
{
    public partial class UJobListView : UserControl
    {
        private System.Windows.Forms.Timer updateTimer;

        public UJobListView()
        {
            InitializeComponent();
            InitTimer();
        }

        public void AddJob(UploadRequestor uJob)
        {
            uJobList.Items.Add(new UJobItem(uJob));
        }

        public void RemoveJob(UploadRequestor uJob)
        {
            System.Collections.IEnumerator itemEnumerator = uJobList.Items.GetEnumerator();
            UJobItem jobItem = null;

            while (itemEnumerator.MoveNext())
            {
                UJobItem item = (UJobItem)itemEnumerator.Current;
                if (item.UJob.Equals(uJob))
                {
                    jobItem = item;
                    break;
                }
            }

            if (jobItem != null)
            {
                RemoveJobItem(jobItem);
            }
        }

        private void RemoveJobItem(UJobItem jobItem)
        {
            uJobList.Items.Remove(jobItem);
        }

        public void StartUpload()
        {
            if (uJobList.SelectedItems.Count > 0)
            {
                System.Collections.IEnumerator enumerator = uJobList.SelectedItems.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    UJobItem jobItem = (UJobItem)enumerator.Current;
                    Thread thread = new Thread(new ThreadStart(jobItem.UJob.StartUpload));
                    thread.Start();
                }
            }
        }

        public void StopUpload()
        {
            if (uJobList.SelectedItems.Count > 0)
            {
                System.Collections.IEnumerator enumerator = uJobList.SelectedItems.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    UJobItem jobItem = (UJobItem)enumerator.Current;
                    Thread thread = new Thread(new ThreadStart(jobItem.UJob.StartUpload));
                    thread.Start();
                }
            }
        }

        private void InitTimer()
        {
            updateTimer = new System.Windows.Forms.Timer();
            updateTimer.Tick += new EventHandler(UpdateTimer_Tick);
            updateTimer.Interval = 2000;
            updateTimer.Start();
        }

        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            RefreshJobsInfo();
        }

        private void RefreshJobsInfo()
        {
            foreach (UJobItem item in uJobList.Items)
            {
                item.RefreshJobInfo();                
            }
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartUpload();
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StopUpload();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (uJobList.SelectedItems.Count > 0)
            {
                System.Collections.IEnumerator enumerator = uJobList.SelectedItems.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    UJobItem jobItem = (UJobItem)enumerator.Current;
                    RemoveJobItem(jobItem);
                }
            }
        }
        
    }
}
